
USE MyMediaStore_DataStaging;

IF OBJECT_ID('dbo.sp_getMinDateOltp') IS NOT NULL
	DROP PROCEDURE dbo.sp_getMinDateOltp;

GO
CREATE PROCEDURE sp_getMinDateOltp
AS
BEGIN

	SET NOCOUNT ON;
	
	--subtrair uma hora para considerar intervalos do tipo ]hh;hh]
	
	select DATEADD(hour,-1,MIN(creation)) as min_date_oltp	
	from (
		SELECT MIN(creation) AS creation 
		  FROM dbo.Address
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Album
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Artist
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.City
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Country
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Customer
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Demographics
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Employee
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Genre
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Invoice
		UNION 
		SELECT MIN(InvoiceDate)  AS creation
		  FROM dbo.Invoice
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.InvoiceLine
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.MediaType
		UNION 
		SELECT MIN(paymentDate) AS creation
		  FROM dbo.Payment
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.PaymentType
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Playlist
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.PlaylistTrack
		UNION 
		SELECT MIN(creation) AS creation 
		  FROM dbo.Track
	) as  dates_oltp
    
END

